<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('债务人')" prop="debtor">
            <el-input v-model="form.debtor" :disabled="disabled" :placeholder="$t('债务人')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('债券一级类别')" prop="bondPrimaryClass">
            <dictSelect
              v-model="form.bondPrimaryClass"
              dictionary="deal_bond_NW"
              :placeholder="$t('债券一级类别')"
              :disabled="disabled"
              @change="primaryClassChange"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('债券二级类别')" prop="bondSecondaryClass">
            <dict-select
              v-if="showBondSencodStatus"
              v-model="form.bondSecondaryClass"
              :dictionary="'deal_bond_NW_' + form.bondPrimaryClass"
              :placeholder="$t('债券二级类别')"
              :disabled="disabled"
              multiple
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('授信额度')" prop="creditLine">
            <Money v-model="form.creditLine" :placeholder="$t('授信额度')" unit="w" :disabled="disabled" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('授信截止日期')" prop="creditDeadline">
            <el-date-picker
              v-model="form.creditDeadline"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('授信截止日期')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" :disabled="disabled" :maxlength="1000" :placeholder="$t('备注')" />
          </el-form-item>
        </el-col>
      </el-row>
      <FileTable :list.sync="form.fileList" data-type="deal_bond_credit" :business-id="bondId" :btn-show="!otherBtnHide" />
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <FormBtn ref="formBtn" :bus-data="form" has-permi="bond:credit" />
    </div>
  </div>
</template>

<script>
import { saveDealBondCredit, getDealBondCredit } from '@/api/bond/credit/dealBondCredit'

export default {
  props: {
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        actTitle: { required: true, message: this.$t('必填信息'), trigger: 'blur' },
        debtor: { required: true, message: this.$t('必填信息'), trigger: 'blur' },
        bondPrimaryClass: { required: true, message: this.$t('必填信息'), trigger: 'blur' },
        bondSecondaryClass: { required: true, message: this.$t('必填信息'), trigger: 'blur' },
        creditDeadline: { required: true, message: this.$t('必填信息'), trigger: 'blur' },
        creditLine: { required: true, message: this.$t('必填信息'), trigger: 'blur' }
      },
      // 遮罩层
      loading: false,
      // 操作按钮默认不展示
      buttonShow: true,
      bondId: this.$route.query.id,
      // 是否隐藏其它按钮
      otherBtnHide: false,
      showBondSencodStatus: true,
      actTitlePrefix: this.$t('新增授信')
    }
  },
  created() {
    this.getDicts('deal_bond_NW').then(response => {
      this.bondPrimaryClassOptions = response.data
    })
    this.$bus.$emit('async_done')
  },
  methods: {
    // 初始化数据
    init(data, isChange) {
      this.loading = true
      if (data != null && data.id !== undefined) {
        getDealBondCredit(data.id).then(response => {
          this.form = response.data
          if (isChange) {
            this.actTitlePrefix = this.$t('变更授信')
            this.form.actStatus = undefined
            if (this.disabled) {
              this.actTitlePrefix = this.$t('续授信')
            }
          }
          this.loading = false
        })
      } else {
        this.reset()
        this.loading = false
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,

        actTitle: undefined,

        bondId: this.bondId,

        debtor: undefined,

        bondPrimaryClass: undefined,

        bondSecondaryClass: undefined,

        creditLine: undefined,

        creditDeadline: undefined,

        record: undefined,

        remark: undefined,
        fileList: []

      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm: function(type) {
      return new Promise((resolve, reject) => {
        this.$refs['form'].validate(valid => {
          // 验证数据
          if (valid) {
            // 弹出遮罩层
            this.loading = true
            // 保存数据
            saveDealBondCredit(this.form).then(response => {
              if (response.code === 200) {
                // 点击的按钮是保存并启动流程
                if (type === 'process') {
                  // 启动流程
                  this.startProcess(response.data.id)
                  // 点击的仅保存
                } else {
                  this.msgSuccess(this.$t('保存成功'))
                  // 关闭form页面
                  this.closeForm()
                }
                resolve()
              } else {
                this.msgError(response.msg)
                this.loading = false
                reject()
              }
            })
          } else {
            reject()
            this.locationError(document)
          }
        })
      })
    },
    // 启动流程
    startProcess: async function(dataId) {
      this.loading = false
      // 启动流程时候传入的参数
      const processData = {}
      // 待办标题
      processData.title = this.actTitlePrefix + ': ' + this.$t('债务人') + '-' + this.form.debtor + ' ' + this.$t('授信额度') +
        '-' + this.moneyFormat(this.form.creditLine, 'w') + ' ' + this.$t('授信截止日期') + ':' + this.parseTime(this.form.creditDeadline)
      // 业务表名
      processData.dataTable = this.$t('deal_bond_credit')
      // 业务ID
      processData.dataId = dataId
      // 业务表单地址(待办页面需要打开的页面)
      processData.formPath = '@/views/bond/credit/dealBondCreditForm'
      // 菜单标识(根据菜单标识判断的走哪个流程)
      processData.menuKey = this.$t('deal_bond_credit')
      this.startProcessByMenu(processData, () => {
        this.form.actStatus = 'D'
        this.msgSuccess(this.$t('数据保存成功,流程启动成功'))
        this.closeForm()
      })
    },
    primaryClassChange() {
      this.showBondSencodStatus = false
      this.form.bondSecondaryClass = undefined
      this.$nextTick(() => {
        this.showBondSencodStatus = true
      })
    }
  }
}
</script>
